// Copyright (c) 1996-2002 John Lyon-Smith. All rights reserved.

#pragma once

#include <wxtl\SyncObject.h>

namespace wxtl
{
	class Thread : public SyncObject
	{
	public:
		Thread()
		{
		}
		Thread(Thread &thread) throw() :
			SyncObject(thread)
		{
		}
		explicit Thread(HANDLE hThread) throw() :
			SyncObject(hThread)
		{
		}
		virtual ~Thread()
		{
		}

		static void staticThreadProc(Thread *pThis)
		{
			pThis->threadProc();
		}

		virtual void threadProc() = 0;

		virtual bool create(bool bSuspend = false)
		{
			DWORD id;

			return (m_h = CreateThread(NULL, 0,
				(LPTHREAD_START_ROUTINE)staticThreadProc, this, 
				bSuspend ? CREATE_SUSPENDED : 0, &id)) != NULL;
		}

		virtual void exit(DWORD exitCode = 0)
		{ 
			ExitThread(exitCode); 
		}
		DWORD exitCode()
		{
			_ASSERTE(isValid());
			DWORD exitCode = 0;
			GetExitCodeThread(m_h, &exitCode);
			return exitCode;
		}
		void suspend() 
		{ 
			SuspendThread(m_h); 
		}
		void resume()
		{ 
			ResumeThread(m_h); 
		} 
		void join()
		{
			wait();
		}
		void kill()
		{
			TerminateThread(m_h,0);
		}
	};				
}
